﻿using System;
using GameEngine.Model;
using GameEngine.Model.Data;
using GameEngine.View;
using GameEngine.View.Components;
using PureMVC.Interfaces;
using PureMVC.Patterns;

namespace GameEngine.Controller
{
    public class CreateWorldCommand<MyIdType, MyBaseUIElementType> : SimpleCommand
        where MyIdType : IComparable<MyIdType>
    {
        override public void Execute(INotification notification)
        {
            IWorldVO<MyIdType> world = notification.Body as IWorldVO<MyIdType>;

            #region DataProxys
            var worldDataProxy = ModelFacade<MyIdType>.Instance.CreateWorldDataProxy(
                world);
            ApplicationFacade.Instance.RegisterProxy(worldDataProxy);

            var mapDataProxy = ModelFacade<MyIdType>.Instance.CreateMapDataProxy(
                    world.Map,
                    ModelFacade<MyIdType>.Instance.Engine.Game.VO.TileSize,
                    ModelFacade<MyIdType>.Instance.Engine.Game.VO.MaxHeight);
            ApplicationFacade.Instance.RegisterProxy(mapDataProxy);
            #endregion

            #region Mediators
            var itemMed = ViewFacade<MyIdType, MyBaseUIElementType>.Instance.CreateItemMediator(
                worldDataProxy);
            ApplicationFacade.Instance.RegisterMediator(itemMed);

            var mapMed = ViewFacade<MyIdType, MyBaseUIElementType>.Instance.CreateMapMediator(
                worldDataProxy,
                ViewFacade<MyIdType, MyBaseUIElementType>.Instance.CreateMapView());
            ApplicationFacade.Instance.RegisterMediator(mapMed);

            var worldMediator = ViewFacade<MyIdType, MyBaseUIElementType>.Instance.CreateWorldMediator(
                    worldDataProxy,
                    mapMed,
                    itemMed,
                    ViewFacade<MyIdType, MyBaseUIElementType>.Instance.CreateWorldView()
                    );
            ApplicationFacade.Instance.RegisterMediator(worldMediator);
            #endregion

            foreach (var thing in world.Items)
            {
                var thingDataProxy = ApplicationFacadeHelper<MyIdType>.CreateThing(thing, world.Id);
                worldDataProxy.InitializeItem(thingDataProxy);

                //ViewFacade<MyGameView, MyLevelView, MyWorldView, MyMapView, MyThingView, MyIdType, MyBaseUIElementType>.Helper.AddThingToWorld(thing, world);
            }

            //SendNotification(ApplicationFacade.WORLDADDED_NOTIFICATION, world);
        }
    }
}
